#include "Player.h"
#include "ImgMgr.h"
#include "Level.h"
#include "Tile.h"

Player::Player()
{

}

void Player::init(ImgMgr *imageManager)
{
    m_currentRender = 0;
    m_direction = 1;
    m_heart = 5;
    m_energy = 10;
    m_dead = false;
    m_skin[0] = imageManager->getSprite(81);
    m_skin[1] = imageManager->getSprite(82);

    m_skin[2] = imageManager->getSprite(83);
    m_skin[3] = imageManager->getSprite(84);

    m_skin[4] = imageManager->getSprite(85);
    m_skin[5] = imageManager->getSprite(86);

    m_skin[6] = imageManager->getSprite(87);
    m_skin[7] = imageManager->getSprite(88);

    m_skin[8] = imageManager->getSprite(89);
    m_skin[9] = imageManager->getSprite(90);

    int e = 0;
    while(e < 10)
    {
        m_skin[e].setScale(2, 2);
        e++;
    }

    m_bufferFire.loadFromFile("ressource/fire.wav");
    m_soundFire.setBuffer(m_bufferFire);
}

int Player::getHeart() const
{
    return m_heart;
}

int Player::getEnergy() const
{
    return m_energy;
}

void Player::modifyEnergy(const float energy)
{
    m_energy += energy;
    if(m_energy > 10)
        m_energy = 10;
}

void Player::modifyHeart(const int heart)
{
    m_heart += heart;
    if(m_heart > 5)
        m_heart = 5;
    if(m_heart <= 0)
        m_dead = true;

}

void Player::setPosition(const sf::Vector2f position)
{
    int e = 0;
    while(e < 10)
    {
        m_skin[e].setPosition(position);
        e++;
    }
}

sf::Vector2f Player::getPosition() const
{
    return m_skin[m_currentRender].getPosition();
}

float Player::random(float a, float b)
{
    return ( rand()/(double)RAND_MAX ) * (b-a) + a;
}

void Player::event(Level *level, sf::View *view)
{
    if(!m_dead)
    {
        bool collisionG = level->entityCollisionTile(getPosition(), 1, 1);
        bool collisionD = level->entityCollisionTile(getPosition(), 1, 2);
        bool collisionB = level->entityCollisionTile(getPosition(), 1, 4);
        bool collisionLader = level->entityInTile(getPosition(), 0, "Ladder", sf::Vector2f(16, 16));
        bool collisionPortal = level->entityInTile(getPosition(), 0, "Portal", sf::Vector2f(16, 16));

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            m_direction = 1;
            if(m_clockFrame.getElapsedTime().asMilliseconds() > 200)
            {
                m_currentRender++;
                if(m_currentRender < 6 || m_currentRender > 7)
                    m_currentRender = 6;
                m_clockFrame.restart();
            }
            if(!collisionG)
                m_skin[m_currentRender].move(-2, 0);
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            m_direction = 2;
            if(m_clockFrame.getElapsedTime().asMilliseconds() > 200)
            {
                m_currentRender++;
                if(m_currentRender < 4 || m_currentRender > 5)
                    m_currentRender = 4;
                m_clockFrame.restart();
            }
            if(!collisionD)
                m_skin[m_currentRender].move(2, 0);
        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            if(collisionLader)
            {
                if(m_clockFrame.getElapsedTime().asMilliseconds() > 200)
                {
                    m_currentRender++;
                    if(m_currentRender < 8 || m_currentRender > 9)
                        m_currentRender = 8;
                    m_clockFrame.restart();
                }
                m_skin[m_currentRender].move(0, -4);
            }
        }
        else
        {

            if(m_clockFrame.getElapsedTime().asMilliseconds() > 200)
            {
                m_currentRender++;
                if(m_direction == 2)
                {
                    if(m_currentRender > 1)
                        m_currentRender = 0;
                }
                else if(m_direction == 1)
                {
                    if(m_currentRender < 2 || m_currentRender > 3)
                        m_currentRender = 2;
                }
                m_clockFrame.restart();
            }
        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        {
            if(m_clockFire.getElapsedTime().asMilliseconds() > 150)
            {
                if(m_energy > 0)
                {
                    if(m_direction == 1)
                        level->addBall(sf::Vector2f(getPosition().x + 16, getPosition().y + 16), -1, random(-0.05, 0.05));
                    else
                        level->addBall(sf::Vector2f(getPosition().x + 16, getPosition().y + 16), 1, random(-0.05, 0.05));

                    m_energy -= 0.5;
                    m_soundFire.play();
                    m_clockFire.restart();
                }
                if(m_energy < 0)
                    m_energy = 0;
            }
        }

        if(!collisionB)
            m_skin[m_currentRender].move(0, 2);

        if(collisionPortal)
        {
            std::cout << "LOAD \n";
            level->loadLevel("ressource/Level", this);
        }

        if(m_energy > 8 && m_clockRegain.getElapsedTime().asMilliseconds() > 2500)
        {
            modifyHeart(1);
            m_clockRegain.restart();
        }

        int e = 0;
        while(e < 10)
        {
            if(e != m_currentRender)
                m_skin[e].setPosition(m_skin[m_currentRender].getPosition());
            e++;
        }
        view->setCenter(m_skin[m_currentRender].getPosition().x, m_skin[m_currentRender].getPosition().y);
    }
}

void Player::render(sf::RenderWindow *app)
{
    if(!m_dead)
        app->draw(m_skin[m_currentRender]);
}

